import 'package:dio/dio.dart';
import 'package:get/get.dart';
import 'package:xiaomi/app/models/categoryModel.dart';
import 'package:xiaomi/app/modules/tools/httpClient.dart';

class CategoryController extends GetxController {
  //TODO: Implement CategoryController

  RxList<CategoryItemModel> catelistData = <CategoryItemModel>[].obs;
  RxList<CategoryItemModel> catelistData2 = <CategoryItemModel>[].obs;
  RxInt selectIndex = 0.obs;
  @override
  void onInit() {
    super.onInit();

    _getCategoryData();
  }

  @override
  void onReady() {
    super.onReady();
  }

  @override
  void onClose() {
    super.onClose();
  }

  void _getCategoryData() async {
    var rsp = await HttpClient.get('api/pcate');
    if (rsp != null) {
      catelistData.value = CategoryModel.fromJson(rsp.data).result!;

      // 根据sID获取二级列表
      _getCategoryData2(catelistData[0].sId!);
      update();
    }
  }

  void _getCategoryData2(String sid) async {
    var rsp = await HttpClient.get('api/pcate?pid=${sid}');
    if (rsp != null) {
      catelistData2.value = CategoryModel.fromJson(rsp.data).result!;

      update();
    }
  }

  void updateSelectIndex(int index) {
    selectIndex.value = index;
    _getCategoryData2(catelistData[index].sId!);
    update();
  }
}
